package com.xykj.enterprise.wechat.event.proc.identity;

import com.alibaba.fastjson.JSONObject;
import com.xykj.enterprise.wechat.bean.busi.vo.wxpush.CreateAuthVo;
import com.xykj.enterprise.wechat.bean.ext.identity.GetPermanentCodeVo;
import com.xykj.enterprise.wechat.event.config.ExtClient;
import com.xykj.enterprise.wechat.event.service.CorpService;
import com.xykj.enterprise.wechat.event.service.SuiteTicketService;
import com.xykj.enterprise.wechat.event.service.SysParamService;
import com.xykj.enterprise.wechat.util.Const;
import com.ydn.its.ItsProc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化认证企业信息，生成永久授权码
 *
 * @Author zhouxu
 * @create 2021-04-01 10:22
 */
@Slf4j
public class CreateAuthProc implements ItsProc {

    @Autowired
    private ExtClient extClient;
    @Autowired
    private SuiteTicketService suiteTicketService;
    @Autowired
    private CorpService corpService;

    @Override
    public void execute(String message) throws Exception {
        log.debug(message);
        CreateAuthVo vo = JSONObject.parseObject(message, CreateAuthVo.class);

        Map params = new HashMap();
        params.put("suite_id", vo.getSuiteId());
        params.put("auth_code", vo.getAuthCode());
        // TODO wap 层管理基本信息，无法传递到 event 层
        params.put("suite_secret", "ofM06Gdx3_7cEyTxFsm9t6Su9emk06TW5doQxbz9oGg");
        params.put("suite_ticket", suiteTicketService.getLatest(vo.getSuiteId()).getSuitTicket());

        // 获取永久授权码
        GetPermanentCodeVo getPermanentCodeVo = extClient.get("GetPermanentCode", params, GetPermanentCodeVo.class);

        // 存储企业信息、永久授权码
        corpService.save(getPermanentCodeVo);
        // 初始化企业扩展信息
        corpService.initExtInfo(getPermanentCodeVo.getAuth_corp_info().getCorpid());

    }
}